﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Reflection;

namespace MDSAdministration
{
    public class StackPanelBuilder : EntityBuilder
    {
        public delegate void EventHandler(Object sender, EntityPropertyEventArgs e);
        public event EventHandler PropertiesHandler;

        private StackPanel stackPanel;
        private object entity;
        private int columnMargin = 150;

        public void BuildStackPanel(object p, StackPanel sp)
        {
            stackPanel = sp;
            stackPanel.Children.Clear();
            entity = p;
            Type t = p.GetType();
            BuildEntity(t);
        }

        new public void BuildEntity(Type t)
        {
            TypeName = t.Name;
            PropertyInfo[] propertyInfos = t.GetProperties();

            foreach (PropertyInfo p in propertyInfos)
            {
                PropertyName = p.Name;
                switch (p.PropertyType.ToString())
                {
                    case "System.String":
                    case "System.Int32":
                    case "System.Byte":
                    case "System.Nullable`1[System.Byte]":
                    case "System.Nullable`1[System.Int32]":
                        buildTextBoxHolder(p);
                        break;
                    case "System.Boolean":
                    case "System.Nullable`1[System.Boolean]":
                        buildCheckBoxHolder(p);
                        break;
                    default:
                        break;
                }
            }
        }

        public override void buildTextBoxHolder(PropertyInfo p)
        {
            Grid g = createGrid();
            g.Children.Add(createLable(PropertyName));

            
            TextBox tb = createTextBox(PropertyName, false);
            setControlProperties(Name, tb);
            g.Children.Add(tb);

            stackPanel.Children.Add(g);
        }

        private TextBox createTextBox(string name, bool isReadOnly)
        {
            TextBox tb = new TextBox();
            Binding textBoxBinding = new Binding(name);
            textBoxBinding.Source = entity;
            textBoxBinding.TargetNullValue = String.Empty;
            tb.SetBinding(TextBox.TextProperty, textBoxBinding);

            tb.Name = "txt" + name;
            tb.IsEnabled = !isReadOnly;
            tb.Margin = new Thickness(columnMargin, 6, 0, 0);
            tb.AcceptsReturn = true;
            return tb;
        }



        private void setControlProperties(string name, Control control)
        {
            if (PropertiesHandler != null)
            {
                EntityPropertyEventArgs e = new EntityPropertyEventArgs() { Name = name, ReadOnly = false, Visibility = true };
                PropertiesHandler(this, e);
                if (e.ReadOnly)
                {
                    control.IsEnabled = !e.ReadOnly;
                }
            }
        }

        public override void buildCheckBoxHolder(PropertyInfo p)
        {
            Grid g = createGrid();

            g.Children.Add(createLable(PropertyName));
            CheckBox cb = createCheckBox(PropertyName);
            setControlProperties(Name, cb);
            g.Children.Add(cb);
            stackPanel.Children.Add(g);
        }
        private Grid createGrid()
        {
            Grid g = new Grid();
            return g;
        }

        private Label createLable(string name)
        {
            Label l = new Label();
            l.Name = "lb" + name;
            l.Content = name;
            l.Margin = new Thickness(6, 6, 0, 0);

            return l;
        }

        private CheckBox createCheckBox(string name)
        {
            CheckBox cbx = new CheckBox();

            Binding checkBoxBinding = new Binding(name);
            checkBoxBinding.Source = entity;
            cbx.SetBinding(CheckBox.IsCheckedProperty, checkBoxBinding);
            cbx.Name = "cbx" + name;
            cbx.Margin = new Thickness(columnMargin, 10, 0, 0);

            return cbx;
        }

        private ComboBox createComboBoxBox(string name, string entityName)
        {
            var cbx = new ComboBox();
            cbx.Name = "cbx" + name;

            cbx.Margin = new Thickness(columnMargin, 6, 0, 0);
            
            return cbx;
        }
    }
}
